<?php
namespace Home\Controller;

class SelfServiceController extends HomeController {
	protected $config = array('app_type' => 'personal');
	
	// 过滤查询字段

    function _search_filter(&$map) {
		if (!empty($_POST["keyword"])) {
		    $this->assign('keyword',$_POST['keyword']);
		   
			$map['name'] = array('like', "%" . $_POST['keyword'] . "%");
		}
		
		//$map['is_del'] = array('eq', '0');		
	}

	
	
	public function index() {
	    $this -> display();
		//$this -> redirect('SelfService/index');
	}
	
	
	/**加班**/
	public function addovertime(){
	    
	    $this -> display();
	    
	}
	
	
	/**请假**/
	
	public function addleave(){
	    $this->_assignLeaveType();
	    $vo=['start_time'=>time(),'end_time'=>time(),'comeback_time'=>time()];
	    $this->assign('leave',$vo);
	    $this -> display();
	}
	
	
	
	public function editleave(){
	    $this->_assignLeaveType();
	    $id = I('id');
	    if(!$id||!is_numeric($id)){
	        $this->error('非法操作');
	        exit;
	    }
	    $mode = D('Leaves');
	    $leave = $mode->where(['id'=>$id])->find();
	    $this->assign('leave',$leave);
	    $this->assign('edit',1);
	    $this -> display('addleave');
	}
	public function saveleave(){
	    
	   // print_r($_POST);exit;
	  
	    if($_POST){
	        $postData=[];
	        $postData['type']=I('type');
	        $postData['confirm']=I('confirm');
	        $postData['confirm_name']=I('confirm_name');
	        $postData['start_time']=strtotime(I('start_time'));
	        $postData['end_time']=strtotime(I('end_time'));	      
	        $postData['count_time_h']=I('count_time_h');
	        $postData['deductions_day']=I('deductions_day');
	        $postData['comeback_time']=strtotime(I('comeback_time'));
	        $postData['create_time']=time();
	        $postData['update_time']=time();
	        $postData['step']=20;
	        $postData['udf_data']=I('content');
	        $postData['user_id']=session('auth_id');
	        $postData['emp_no']=session('emp_no');
	        $postData['user_name']=session('user_name');
	        $postData['dept_id']=session('dept_id');
	        $leave = D('Leaves');
	        $id = I('id');
	        if($id){
	            $postData['id']=$id;
	            $res = $leave->do_update($postData);
	            if($res['code']){
	                $this -> error($res['msg']);
	                exit;
	            }
	        }else{
	            $res = $leave->do_save($postData);
	        }
	        
	         if($res){
	          $this -> success("提交成功");
	        } else{
	            $this -> error("添加失败..");
	        }
	        
	    }
	   
	   exit;
	    
	}

	/**请假记录**/
	public function leavelist(){	   
	    
	    $model = D("Leaves");
	    $map = $this -> _search($model);
	    $user_id = get_user_id();
	    $map['user_id'] =$user_id;
    	if(I('keyword')){
    	   $map['udf_data'] = array('like', '%' . trim(I('keyword')) . '%');    	    
    	 } 
	     
	    $model = D("Leaves");
	    if (!empty($model)) {
	        $this -> _list($model, $map, 'id desc');
	    }

	    $this->_assignLeaveType(1);
	    $this->display();
	    
	}
	
	
	/*自助服务审核列表*/
	public function folder(){
	    $model = D("LeaveView");
	    $map = $this -> _search($model);
	    $map['uid']=get_user_id();
	  
	    if (!empty($model)) {
	        $this -> _list($model, $map, '');
	    }
	    
	    $this->display();
	    
	    
	}
	
	
	
	
	public function read($id){
	     $uid = get_user_id();
	    $this->_assignLeaveType();
	    $this->assign('is_edit',1);
	    $this->assign('save',0);
	    $logmodel = D('LeaveLog');
	    $log= $logmodel->where("flow_id={$id} and uid={$uid} and is_del=0")->find();
	    if(!$log['result']){
	       $this ->assign("to_confirm", 1);
	     }
	    $this->assign('log',$log);
	    $logs= $logmodel->where("flow_id={$id} and result is not null ")->select();
	    //print_r($logs);exit;
	    $this->assign('flow_log',$logs);
	    $model = D('Leaves');
	    $leave = $model->where(['id'=>$id])->select()[0]; 
	    $this->assign('leave',$leave);
	    $this->assign('read',"readonly='readonly'");
	    
	    $this->display();
	    
	}
	
	
	/*审批通过*/
	public function approve() {
	   
	    $flow_id=I('flow_id');
	    $id = I('id');
	    $mode = D('LeaveLog');
	    $mode->result=1;
	    $mode->comment=I('comment');
	    $step = I('step');
	    
	    
	    $list= $mode->where("id={$id}")->save();
	    if ($list !== false) {//保存成功

	        D("Leaves") -> next_step($flow_id, $step);
	        $this -> assign('jumpUrl', U('self_service/folder', 'fid=confirm'));
	        $this -> success('操作成功!');
	    } else {
	        //失败提示
	        $this -> error('操作失败!');
	    }
	}
	
	
	/*审批不通过*/
	public function reject() {

	    $flow_id=I('flow_id');
	    $id = I('id');
	    $mode = D('LeaveLog');
	    $mode->result=0;
	    $mode->comment=I('comment');
	    $step = I('step');
	     

	    $where['step'] = array('eq', $step);
	    $where['flow_id'] = array('eq', $flow_id);
	    $where['_string'] = 'result is null';
	   
	     $res= $mode->where("id={$id}")->save();
	    if ($res !== false) {//保存成功
	
	        M("Leaves") -> where("id=$flow_id") -> setField('step', 0);
	        $flow = M("Leaves") -> find($flow_id);
	
	        $push_data['type'] = '审批';
	        $push_data['action'] = '被否决';
	        $push_data['title'] = $flow['name'];
	        $push_data['content'] = '审核人：' . get_dept_name()."-".get_user_name();
	        $push_data['url']=U('SelfService/read',"id={$flow['id']}&fid=submit&return_url=SelfService/index");
	        send_push($push_data, $flow['user_id']);
	
	        $this -> assign('jumpUrl', U('SelfService/folder', 'fid=confirm'));
	        $this -> success('操作成功!');
	    } else {
	        //失败提示
	        $this -> error('操作失败!');
	    }
	}
	
	function _assignLeaveType($type=''){
	    $code = 'leavetype';
	    $typeList = M("SystemConfig") -> where("code='{$code}'") -> select()[0]['val'];
	    $typeList = json_decode($typeList,1);
	    if($type){
	        $ntlist = [];
	        foreach ($typeList as $tem){
	            $ntlist[$tem['key']]=$tem['value'];
	        }
	        $this->assign('typelist',$ntlist);
	    }
	    else
	    $this->assign('typelist',$typeList);
	    
	}
	
	
	function userlist(){
	     
	    $node = D("Dept");
		$menu = array();
		$menu = $node -> field('id,pid,name') -> where("is_del=0") -> order('sort asc') -> select();
		$tree = list_to_tree($menu);	
		$list = tree_to_list($tree);	
	    $this->assign('d_list',$list);
	    $model = D('Position');
	    $plist =$model->where('is_del = 0 ')->order('sort ')->select();
	    $this->assign('plist',$plist);
	    $map=[];
	    if(I('sex')){
	        $map['sex']=array('eq',I('sex'));
	        $this->assign('sex',I('sex'));
	    }
	    if(I('dept')){
	        $map['dept_id']=array('eq',I('dept'));
	        $this->assign('dept_id',I('dept'));
	    }
	    if(I('pid')){
	        $map['position_id']=array('eq',I('pid'));
	        $this->assign('pid',I('pid'));
	    }
	    
	    $this->_search_filter($map);
	    $map['emp_no']=array('neq','admin');
	    $model = D("UserView");
	    $res= $this -> _list($model, $map, 'id desc');
	    $this->display();
	    
	}
	

}


	
